<?php

/**
 * @author      :   TrongNV
 * @name        :   My_Business_Storage_Category
 * @version     :   1.0
 * @copyright   :   VinhLoi
 */
class My_Business_Storage_Category {

    /**
     *
     * @var <type>
     */
    protected static $_instance = null;

    /**
     * table name
     * @var string $_table
     */
    private static $_table = "category";

    /**
     * Constructor of class
     * we don't permit an explicit call of the constructor! (like $v = new Singleton())
     */
    protected function __construct() {
        
    }

    /**
     * Get singleton instance
     * @return My_Business_Storage_Category
     */
    public final static function getInstance() {
        //Check instance
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }

        //Return instance
        return self::$_instance;
    }

    /**
     * Insert category
     * @param int $p_intUserID
     * @param array $p_arrData
     * @return int $result : last inserted ID
     */
    public function insert($p_intUserID, $p_arrData) {
        try {
            //Get DB GLOBAL
            $storage = My_Global::getDbMasterBusiness();

            //$intID = My_Global::getMaxID('coupon');
            //$p_arrData['coupon_id'] = $intID;
            $p_arrData['user_id'] = $p_intUserID;
            //Insert Data
            $result = $storage->insert(self::$_table, $p_arrData);

            if ($result) {
                $result = $intID;
            }

            //Return
            return $result;
        } catch (Exception $e) {
            return 0;
        }
    }

    /**
     * Delete category
     * @param int $p_intUserID
     * @param int $p_intCategoryID
     * @return int $result : 1 success
     */
    public function delete($p_intUserID, $p_intCategoryID) {
        try {
            //Get DB GLOBAL
            $storage = My_Global::getDbMasterBusiness();

            //Delete data
            $result = $storage->delete(self::$_table, 'user_id=' . $p_intUserID . ' AND category_id=' . $p_intCategoryID);

            return $result;
        } catch (Exception $e) {
            return 0;
        }
    }

    /**
     * Get list category by condition
     * @param array $p_arrCondition
     * @return array $result
     */
    public function getListCategory() {
        $result = array();
        //Get DB GLOBAL
        $storage = My_Global::getDbMasterBusiness();
        $select = $storage->select()
                ->from(self::$_table, '*')
                ->where('1=1');
        $result = $storage->fetchAll($select);
        //Return array
        return $result;
    }

    /**
     * Get list category limit by condition
     * @param array $p_arrCondition
     * @param int   $p_intPage
     * @param int   $p_intLimit
     * @return array $result
     */
    public function getListLimit($p_arrCondition, $p_intPage, $p_intLimit) {
        try {
            if (!empty($p_arrCondition)) {
                $strWhere = $this->condition($p_arrCondition);
            }

            //Get DB Business
            $storage = My_Global::getDbMasterBusiness();

            $select = $storage->select()
                    ->from(self::$_table, '*')
                    ->order('article_id DESC')
                    ->where('1=1' . $strWhere)
                    ->limit($p_intLimit, $p_intLimit * ($p_intPage - 1));
            $result = $storage->fetchAll($select);

            //Return array
            return $result;
        } catch (Exception $e) {
            return array();
        }
    }

    /**
     * Build condition string
     * @param array $p_arrCondition
     * @return string $result
     */
    private function condition($p_arrCondition = array()) {
        $result = '';
        if (isset($p_arrCondition['begin_date'])) {
            $result .= " AND begin_date BETWEEN " . $p_arrCondition['begin_date'] . " AND " . ($p_arrCondition['begin_date'] + (60 * 60 * 24 - 1));
        }

        if (isset($p_arrCondition['status'])) {
            $result .= " AND status=" . $p_arrCondition['status'];
        }

        if (isset($p_arrCondition['user_id'])) {
            $result .= " AND article_id=" . $p_arrCondition['article_id'];
        }

        return $result;
    }

}
